import java.util.Arrays;




/**
 * @Description
 */

public class MyArrayList implements IList{
    int usedSize;
    public int[] elem = new int[6];


    @Override
    public void add(int data){
        if(isFull()){
            resize();
        }
        elem[usedSize] = data;
        usedSize++;
    }
    @Override
    public void add(int pos, int data){
        //满了进行扩容
        if(isFull()){
            resize();
        }
        //pos位置不合法
        if(pos<0||pos>=usedSize){
            return;
        }
        //在数组中插入数字
        //数据挪动
        for (int i = usedSize-1; i >=pos ; i--) {
            elem[i+1] = elem[i];
        }
        //存放数据
        elem[pos] = data;
        usedSize++;


    }


    @Override
    public void addFirst(int data) {
        for (int i = usedSize-1; i >=0; i--) {
            elem[i+1] = elem[i];
        }
        elem[0] = data;
        usedSize++;
    }

    @Override
    public void addLast(int data) {
        int i = 0;
        while(i == usedSize-1) {
            elem[i+1] = elem[i];
        }
        elem[0] = data;
        usedSize++;

    }

    @Override
    public void addIndex(int index, int data) {

    }

    @Override
    public boolean contains(int key) {//size
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == key){
                return true;
            }
        }
        return false;

    }

    @Override
    //怎样删除两个同样的数据呢
    public void remove(int key) {
        //删除一个数据
        int index = toFind(key);
        if(toFind(key) == -1){
            return;
        }
        for (int i = index; i < usedSize-1; i++) {
            elem[i] = elem[i+1];
        }
        usedSize--;


    }

    @Override
    public void removeAllKey(int key) {

    }

    @Override
    public int size() {
        return IList.super.size();
    }

    @Override
    public void clear() {
        usedSize = 0;

    }

    @Override
    public void display() {

    }
    public boolean isFull(){
        return usedSize == elem.length;
    }
    private void resize(){
        this.elem = Arrays.copyOf(elem,2*elem.length);
    }

    @Override
    public int get(int pos) {
        if(pos<0||pos>=usedSize){
            return -1;//或者抛出异常
        }
        return elem[pos];
    }

    @Override
    public int toFind(int pos) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == pos){
                return i;
            }
        }
        return -1;

    }
}
